using UnityEngine;
using System.Collections;

/**
 * Handles generation of Player enemy and enemy related prefabs
 */
public class GenerateObjects : MonoBehaviour {
	//-- properties --//
	/*
	 * Prefab ref for enemyOne
	 */
	public GameObject enemyOne;
	
	/*
	 * Prefab ref for enemyTwo
	 */
	public GameObject enemyTwo;
	
	/*
	 * Prefab ref for Player
	 */
	public GameObject playerPrefab;
	
	/**
	 * Prefab ref for pickups.
	 */
	public GameObject pickupPrefab;
	
	private static GameObject cheatEnemyOne;
	private static GameObject cheatEnemyTwo;
	
	private const float playerStartX = -680.5869f;
	private const float playerStartY = 2.272881f;
	public const float playerStartZ = 0; // rail?
	
	// list of all locations/tags of enemies
	public static ArrayList everyEnemy = new ArrayList();
	public static ArrayList originalLocations = new ArrayList();
	public static ArrayList originalTags = new ArrayList();
	
	// track things to remove on spawn
	private ArrayList cleanUpObjects =  new ArrayList();
	private static GameObject realPlayer;
	
	//-- GenerateEnemies methods --//
	
	// Use this for initialization
	void Start () {
		// cheat and copy refs
		cheatEnemyOne = enemyOne;
		cheatEnemyTwo = enemyTwo;
	}
	
	// Update is called once per frame
	void Update () {
	}
	
	/**
	 * Handles enemy layout.
	 **/
	public void spawn() {
		// clean up anything		
		foreach (GameObject killMe in this.cleanUpObjects) {
			RangedShoot em = killMe.GetComponent<RangedShoot>();
			if (em != null)
				em.clearBullets();
			
			Destroy( killMe );	
		}
		this.cleanUpObjects.Clear();
	}
	
	/**
	 * Creates and returns a new EnemyOne positioned at passed x,y
	 **/
	public GameObject createEnemyOne( float x, float y, float z ) {
		return (GameObject) Instantiate( enemyOne, new Vector3(x, y, z), Quaternion.identity);
	}
	
	/**
	 * Statically creates enemyOnes
	 */
	public static GameObject cheatMakeEnemyOne( Vector3 position ) {
		return (GameObject) Instantiate( cheatEnemyOne, position, Quaternion.identity);
	}
	
	/**
	 * Statically creates enemyTwos
	 */
	public static GameObject cheatMakeEnemyTwo( Vector3 position ) {
		return (GameObject) Instantiate( cheatEnemyTwo, position, Quaternion.identity);
	}
	
	/**
	 * Creates and returns a new EnemyTwo positioned at passed x,y
	 **/
	public GameObject createEnemyTwo( float x, float y, float z ) {
		return (GameObject) Instantiate( enemyTwo, new Vector3(x, y, z), Quaternion.identity);
	}
	
	/**
	 * Creates and returns a new pickup positioned at passed x,y
	 **/
	public GameObject createPickupObject( float x, float y ) {
		return (GameObject) Instantiate( pickupPrefab, new Vector3(x, y, playerStartZ), Quaternion.identity);
	}
	
	/**
	 * Returns player object.
	 **/
	public static GameObject getPlayer() {
		return realPlayer;
	}
	
	/**
	 * Sets player object.
	 **/
	public static void setPlayer( GameObject player ) {
		realPlayer = player;
	}
	
	/**
	 * Generates player and places at passed x,y
	 * Returns generated player.
	 * Destorys any previous player.
	 **/
	public GameObject createPlayer( float x, float y, float z ) {
		if (GenerateObjects.realPlayer != null) {
			print ( "positioning" );
			GenerateObjects.realPlayer.transform.position = new Vector3( playerStartX, playerStartY );	
		}
		else {
			print ( "trying to create player..." );
			GenerateObjects.realPlayer = (GameObject) Instantiate( playerPrefab, new Vector3( x, y , z ), Quaternion.identity );
		}
		return GenerateObjects.realPlayer;
	}

}
